#include "StdAfx.h"
#include "PhysXFileOutTerminal.h"
#include "globalfunctions.h"

namespace _MODULE_
{
	namespace _PHYSICS_
	{
		PhysXFileOutTerminal::PhysXFileOutTerminal(_FOGGE_::foString name,_FOGGE_::foUInt size)
			:_FOGGE_::_UTIL_::FileOutTerminal(name,size)
		{
		}

		PhysXFileOutTerminal::~PhysXFileOutTerminal(void)
		{
		}

		void PhysXFileOutTerminal::reportError (NxErrorCode code, const char *message, const char* file, int line)
		{
			//this should be routed to the application
			//specific error handling. If this gets hit
			//then you are in most cases using the SDK
			//wrong and you need to debug your code!
			//however, code may  just be a warning or
			//information.

			_FOGGE_::foString errorStr;

		
			
		#ifdef UNICODE
			_FOGGE_::foString unistr;	
			_FOGGE_::_UTIL_::_HELPER_::AnsiToUnicode(file,unistr);
			errorStr+=unistr;
		#else 
			errorStr+=file;
		#endif
			
			errorStr+=_T(" : ");

			TCHAR linestr[255];

			_ltot(line,linestr,10);

			errorStr+=linestr;

			errorStr+=_T("\t");
			
			switch (code)
			{
			case NXE_INVALID_PARAMETER:
				errorStr+=_T("invalid parameter");
				break;
			case NXE_INVALID_OPERATION:
				errorStr+=_T( "invalid operation");
				break;
			case NXE_OUT_OF_MEMORY:
				errorStr+=_T( "out of memory");
				break;
			case NXE_DB_INFO:
				errorStr+=_T( "info");
				break;
			case NXE_DB_WARNING:
				errorStr+=_T( "warning");
				break;
			default:
				errorStr+=_T("unknown error");
			}

			errorStr+=_T(" : ");
		#ifdef UNICODE
			_FOGGE_::_UTIL_::_HELPER_::AnsiToUnicode(message,unistr);
			errorStr+=unistr;
		#else 
			errorStr+=message;
		#endif
		
			errorStr+=_T("\n");

			Write(errorStr.c_str());

		}
                
		NxAssertResponse PhysXFileOutTerminal::reportAssertViolation (const char *message, const char *file,int line)
		{
			_FOGGE_::foString errorStr;

			errorStr+=_T("access violation : ");
		#ifdef UNICODE
			_FOGGE_::_UTIL_::_HELPER_::AnsiToUnicode(message,unistr);

			errorStr+=unistr;
			
			errorStr+=_T("(");
			_FOGGE_::_UTIL_::_HELPER_::AnsiToUnicode(file,unistr);
			errorStr+=unistr;
			errorStr+=_T("Line");
			
		#else 
			
			errorStr+=message;
			
			errorStr+=_T("(");
			errorStr+=file;
			
			errorStr+=_T("Line");
		#endif

			TCHAR linestr[255];
			_ltot(line,linestr,10);
			errorStr+=linestr;

			errorStr+=_T(")\n");

			Write(errorStr.c_str());

	#ifdef WIN32
			switch (MessageBox(0, message, _T("AssertViolation, see console for details."), MB_ABORTRETRYIGNORE))
				{
				case IDRETRY:
					return NX_AR_CONTINUE;
				case IDIGNORE:
					return NX_AR_IGNORE;
				case IDABORT:
				default:
					return NX_AR_BREAKPOINT;
				}
	#elif LINUX
			assert(0);
	#elif _XBOX
			return NX_AR_BREAKPOINT;
	#elif __CELLOS_LV2__
			return NX_AR_BREAKPOINT;
	#endif
		}
                
		void PhysXFileOutTerminal::print (const char *message)
		{
			_FOGGE_::foString errorStr;

			errorStr+=_T("SDK says: ");

		#ifdef UNICODE
			_FOGGE_::_UTIL_::_HELPER_::AnsiToUnicode(message,unistr);
			errorStr+=unistr;
		#else 
			errorStr+=message;
		#endif

			errorStr+=_T("\n");


			Write(errorStr.c_str());
			
		}
	}
}